<?php
namespace  app\project\controller;
use app\admin\model\Admin_model;
use think\Controller;
use think\Db;
use think\Request;

Class Columnmanage extends Controller{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('user')) {
            $user=session('user');
            $admin_model=new Admin_model();
            $fun=$admin_model->get_rolefunction($user['role']);
            $this->assign('fun',$fun);
            $this->assign('active','栏目管理');
        }else{
            $this->redirect('index/login/index');
        }
    }
    //侧边栏栏目
    public function sidecolumn(){
        $this->assign('title','侧边栏栏目');
        return $this->fetch();
    }
    public function getsidecolumn(){
        $where=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $where['name']=['like','%'.$_GET['search'].'%'];
        }
        if(isset($_GET['role'])&&$_GET['role']!='all'){
            $where['user_role']=$_GET['role'];
        }
        $f=Db::name('function')->where($where)->order('sort','asc')->select();
        $function=array();
        $data=array();
        $role=array('0'=>'管理员','1'=>'代理商','2'=>'子账户','3'=>'广告主');
        foreach ($f as  $k=>$v){
          $data['id']=$v['id'];
          $data['name']=$v['name'];
          $data['url']=$v['url'];
          $data['fu_name']='';
          $data['role']=$role[$v['user_role']];
          $data['level']='1';
          $data['sort']=' <input onchange="edit_sort('.$data['id'].','.$data['level'].',this.value)" value="'.$v['sort'].'" type="text" name="url" style="width: 50px;" class="form-control"  placeholder="顺序"/>';
          $data['s']=$v['state']?'是':'否';
            if($v['state']){
                $data['state']='<a onclick="edit_state('.$data['id'].','.$data['level'].',0)" type="button" class="btn btn-sm btn-primary" >'.$data['s'].'</a>';
            }else{
                $data['state']='<a onclick="edit_state('.$data['id'].','.$data['level'].',1)" type="button" class="btn btn-sm btn-danger" >'.$data['s'].'</a>';
            }
          $data['operation']='<a href="'.url('edit_fun').'?&id='.$data['id'].'&level='.$data['level'].'" type="button" class="btn btn-sm btn-primary" >编辑</a> 
<a onclick="del_fun('.$data['id'].','.$data['level'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
          array_push($function,$data);
        }
        $subwhere=array();
        if(isset($_GET['search'])&&$_GET['search']){
            $subwhere['a.subname|b.name']=['like','%'.$_GET['search'].'%'];
        }
        if(isset($_GET['role'])&&$_GET['role']!='all'){
            $subwhere['b.user_role']=$_GET['role'];
        }
        $subf=Db::name('subfunction')->alias('a')->field('a.*,b.name,b.user_role')
            ->join('function b','a.fu_id=b.id')
            ->where($subwhere)
            ->order('b.sort','asc')
            ->select();
        foreach ($subf as $k =>$v){
            $data['id']=$v['id'];
            $data['name']=$v['subname'];
            $data['url']=$v['suburl'];
            $data['fu_name']=$v['name'];
            $data['sort']='';
            $data['role']=$role[$v['user_role']];
            $data['level']='2';
            $data['s']=$v['state']?'是':'否';
            if($v['state']){
                $data['state']='<a onclick="edit_state('.$data['id'].','.$data['level'].',0)" type="button" class="btn btn-sm btn-primary" >'.$data['s'].'</a>';
            }else{
                $data['state']='<a onclick="edit_state('.$data['id'].','.$data['level'].',1)" type="button" class="btn btn-sm btn-danger" >'.$data['s'].'</a>';
            }
            $data['operation']='<a href="'.url('edit_fun').'?&id='.$data['id'].'&level='.$data['level'].'" type="button" class="btn btn-sm btn-primary" >编辑</a> 
<a onclick="del_fun('.$data['id'].','.$data['level'].')" type="button" class="btn btn-sm btn-danger" >删除</a>';
            array_push($function,$data);
        }
        return array('rows'=>$function,'total'=>count($function));
    }
    //添加侧边栏目
    public function  add_sidcol(){
        if(request()->isPost()){
            $data=input('post.');
            if ($data['fu_id']){
                $subf=array(
                    'subname'=>$data['name'],
                    'suburl'=>$data['url'],
                    'fu_id'=>$data['fu_id']
                    );
                $id=Db::name('subfunction')->insertGetId($subf);
                if($id){
                    return array('code' => 1, 'msg' => '添加成功', 'url' => url('sidecolumn'));
                }else{
                    return array('code' => 0, 'msg' => '添加失败',);
                }

            }else{
                $ff=array(
                    'name'=>$data['name'],
                    'url'=>$data['url'],
                    'user_role'=>$data['role'],
                    'sort'=>$data['sort']
                );
                $id=Db::name('function')->insertGetId($ff);
                if($id){
                    return array('code' => 1, 'msg' => '添加成功', 'url' => url('sidecolumn'));
                }else{
                    return array('code' => 0, 'msg' => '添加失败',);
                }
            }
        }

        $f=Db::name('function')->select();
        $role=array('0'=>'管理员','1'=>'代理商','2'=>'子账户','3'=>'广告主');
        foreach ($f as &$v){
            $v['role']=$role[$v['user_role']];
        }
        $this->assign('f',$f);
        $this->assign('title','添加侧边栏目');
        return $this->fetch();
    }
    //删除侧边
    public function del_fun(){
        if($_GET['level']=='1'){
            $info=Db::name('function')->where('id',$_GET['id'])->delete();

            if($info){
                return array('code' => 1, 'msg' => '删除成功' );
            }else{
                return array('code' => 0, 'msg' => '删除失败');
            }
        }else if($_GET['level']=='2'){
            $info=Db::name('subfunction')->where('id',$_GET['id'])->delete();
            if($info){
                return array('code' => 1, 'msg' => '删除成功');
            }else{
                return array('code' => 0, 'msg' => '删除失败');
            }
        }
        return array('code' => 0, 'msg' => '参数有误');
    }
    //修改是否显示
    public function edit_state(){
        if($_GET['level']=='1'){
            $data['state']=$_GET['state'];

            $info=Db::name('function')->where('id',$_GET['id'])->update($data);

            if($info){
                return array('code' => 1, 'msg' => '修改成功' );
            }else{
                return array('code' => 0, 'msg' => '修改失败');
            }
        }else if($_GET['level']=='2'){
            $data['state']=$_GET['state'];
            $info=Db::name('subfunction')->where('id',$_GET['id'])->update($data);
            if($info){
                return array('code' => 1, 'msg' => '修改成功');
            }else{
                return array('code' => 0, 'msg' => '修改失败');
            }
        }
        return array('code' => 0, 'msg' => '参数有误');
    }
    public function edit_sort(){
        if($_GET['level']=='1'){
            $data['sort']=$_GET['sort'];

            $info=Db::name('function')->where('id',$_GET['id'])->update($data);

            if($info){
                return array('code' => 1, 'msg' => '修改成功' );
            }else{
                return array('code' => 0, 'msg' => '修改失败');
            }
        }else if($_GET['level']=='2'){
            $data['sort']=$_GET['sort'];
            $info=Db::name('subfunction')->where('id',$_GET['id'])->update($data);
            if($info){
                return array('code' => 1, 'msg' => '修改成功');
            }else{
                return array('code' => 0, 'msg' => '修改失败');
            }
        }
        return array('code' => 0, 'msg' => '参数有误');
    }
    //
    public function edit_fun(){
        if(request()->isPost()){
                $post=input('post.');
                if($post['fu_id']){
                    $subf=array(
                        'id'=>$post['id'],
                        'subname'=>$post['name'],
                        'suburl'=>$post['url'],
                        'fu_id'=>$post['fu_id']
                    );
                    $info=Db::name('subfunction')->update($subf);
                    if($info){
                        return array('code' => 1, 'msg' => '修改成功', 'url' => url('sidecolumn'));
                    }else{
                        return array('code' => 0, 'msg' => '修改失败',);
                    }
                }else{
                    $ff=array(
                        'id'=>$post['id'],
                        'name'=>$post['name'],
                        'url'=>$post['url'],
                        'user_role'=>$post['role'],
                        'sort'=>$post['sort']
                    );
                    $info=Db::name('function')->update($ff);
                    if($info){
                        return array('code' => 1, 'msg' => '修改成功', 'url' => url('sidecolumn'));
                    }else{
                        return array('code' => 0, 'msg' => '修改失败',);
                    }
                }
        }
        $data=array();
        $role=array('0'=>'管理员','1'=>'代理商','2'=>'子账户','3'=>'广告主');
        if($_GET['level']=='1'){
            $func=Db::name('function')->where('id',$_GET['id'])->find();
            $data['id']=$func['id'];
            $data['name']=$func['name'];
            $data['url']=$func['url'];
            $data['fu_name']='';
            $data['fu_id']='';
            $data['sort']=$func['sort'];
            $data['user_role']=$func['user_role'];
            $data['role']=$role[$func['user_role']];
            $data['level']='1';

        }else if($_GET['level']=='2'){

            $func=Db::name('subfunction')->alias('a')->field('a.*,b.name,b.user_role')
                ->join('function b','a.fu_id=b.id')
                ->where('a.id',$_GET['id'])
               ->find();
            $data['id']=$func['id'];
            $data['name']=$func['subname'];
            $data['url']=$func['suburl'];
            $data['fu_name']=$func['name'];
            $data['fu_id']=$func['fu_id'];
            $data['sort']='';
            $data['user_role']=$func['user_role'];
            $data['role']=$role[$func['user_role']];
            $data['level']='2';
        }
        $this->assign('func',$data);
        $f=Db::name('function')->select();
        $role=array('0'=>'管理员','1'=>'代理商','2'=>'子账户','3'=>'广告主');
        foreach ($f as &$v){
            $v['role']=$role[$v['user_role']];
        }
        $this->assign('f',$f);
        $this->assign('title','编辑侧边栏目');
        return $this->fetch();
    }
}